<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2024/5/7
  Time: 21:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <style>
        .demo-reg-container{width: 320px; margin: 21px auto 0;}
        .demo-reg-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
    </style>
    <form class="layui-form" lay-filter="updateFormFilter" action="">
        <div class="demo-reg-container">
            <input type="hidden" name="id"> <%--隐藏id--%>

            <input type="hidden" name="id" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">

            原密码：
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="oldPassword" value="" lay-verify="required|oldPass" placeholder="" autocomplete="off" class="layui-input" id="oldPassword" lay-affix="eye">
                </div>
            </div>
            新密码：
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="newPassword" value="" lay-verify="required|newPass" placeholder="" autocomplete="off" class="layui-input" id="newPassword" lay-affix="eye">
                </div>
            </div>
            确认密码：
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" placeholder="" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>

            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="submit">修改密码</button>
            </div>
        </div>
    </form>

    <script>
        layui.use(function(){
            var $ = layui.$;
            var form = layui.form;
            var layer = layui.layer;

            //拿到index.jsp传递过来的参数--编辑回显数据
            var queryString = window.location.search;
            var urlParams = new URLSearchParams(queryString);
            var id = urlParams.get("id");

            $.post(
                '/user?method=selectById',
                {'id':id},
                function (result) {
                    //console.log(result);
                    if (result.code == 0){
                        form.val('updateFormFilter',result.data);
                    }
                },
                'json'
            );

            // 自定义验证规则
            form.verify({
                //oldPass
                oldPass:function (value,elem) {
                    if (!/^[\S]{3,9}$/.test(value)){
                        return '密码必须为3到9位的非空字符串';
                    }

                },
                newPass:function (value,elem) {
                    if (!/^[\S]{3,9}$/.test(value)){
                        return '密码必须为3到9位的非空字符串';
                    }
                },
                // 确认密码
                confirmPassword: function(value, item){
                    var passwordValue = $('#newPassword').val();
                    if(value !== passwordValue){
                        return '两次密码输入不一致';
                    }
                }
            });

            //绑定提交事件
            form.on('submit(submit)',function (data) {
                //获取表单字段
                var field = data.field;
                console.log(data);
                // 此处可执行 Ajax 等操作
                $.post(
                    '/user?method=updatePass',
                    field,
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function (){
                                parent.location.href = '/page/login';
                            },2000)
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
                return false;//阻止默认form跳转
            });
        });

    </script>

</body>
</html>
